package com.sinodata.bsm.cicp.collector.sqlserver;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class ServerInfo extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(ServerInfo.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");

            String sql = "select srvname from master.dbo.sysservers";
            String srvname = this.getSrvName(serverName, sql, ip);

            sql = "sp_getsvrinfo"; //
            String[] rs = this.getPropValArr(serverName, sql, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            propValues[0] = ResPropertyValue.createPropValue(srvname, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue("" + rs[0], propMetas[1]);
            propValues[2] = ResPropertyValue.createPropValue("" + rs[1], propMetas[2]);
            propValues[3] = ResPropertyValue.createPropValue("" + rs[2], propMetas[3]);
            propValues[4] = ResPropertyValue.createPropValue("" + rs[3], propMetas[4]);
            propValues[5] = ResPropertyValue.createPropValue("" + rs[4], propMetas[5]);
            propValues[6] = ResPropertyValue.createPropValue("" + rs[5], propMetas[6]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception e) {
            logger.error("failed to collect sqlserver server Info", e);
            throw new CollectorException(e);
        }
        return values;
    }

    private String[] getPropValArr(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[6];
        try {
            ResultSet rst = this.getCachedResultSet("sqlserver", ip, serverName, sql);
            while (rst.next()) {
                String name = rst.getString(2);
                if (name.equals("Platform"))
                    propValArr[0] = rst.getString(4);
                else if (name.equals("ProcessorCount"))
                    propValArr[1] = rst.getString(4);
                else if (name.equals("ProcessorType"))
                    propValArr[2] = rst.getString(4);
                else if (name.equals("PhysicalMemory")) {
                    propValArr[3] = rst.getString(4);
                    //截取括号以前的MB数值
                    int start = propValArr[3].indexOf("(");
                    propValArr[3] = propValArr[3].substring(0, start);

                } else if (name.equals("WindowsVersion"))
                    propValArr[4] = rst.getString(4);
                else if (name.equals("FileVersion"))
                    propValArr[5] = rst.getString(4);
            }
        } catch (Exception ex) {
            logger.error("failed to get property attribute", ex);
            throw new CollectorException(ex);
        }

        return propValArr;
    }

    private String getSrvName(String serverName, String sql, String ip) throws CollectorException {
        StringBuffer srvname = new StringBuffer();

        try {
            ResultSet rst = this.getCachedResultSet("sqlserver", ip, serverName, sql);
            while (rst.next()) {
                String rowCount = rst.getString(1);
                srvname.append(rowCount == null ? "" : rowCount.trim());
            }
        } catch (Exception ex) {
            logger.error("while connect database : ", ex);
            throw new CollectorException(ex);
        }

        return srvname.toString();
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "所属主机", "所属主机的平台", "所属主机的处理器个数", "所属主机的处理器类型", "所属主机的物理内存", "所属主机Windows版本", "SQL Server版本" };
        return new String[] { "BelongToHost", "HostPlatform", "HostProcessesNum", "HostProcessesType", "HostPhysicalMemory", "HostWindowsVersion", "SQLServerVersion" };
    }

}
